/**
 * 配合 tcp_server_time 使用
 *   获取 server 端时间
*/

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <time.h>

#include <arpa/inet.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>

int main(int argc, char **argv)
{

    if (argc < 3)
    {
        fprintf(stderr, "%s <server_ip> <server_port>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    int fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd < 0)
    {
        perror("socket error: ");
        exit(EXIT_FAILURE);
    }

    struct sockaddr_in serverAddr;
    memset(&serverAddr, 0, sizeof(serverAddr));
    serverAddr.sin_family = AF_INET;
    serverAddr.sin_port = htons(atoi(argv[2]));
    serverAddr.sin_addr.s_addr = inet_addr(argv[1]);

    if (connect(fd, (struct sockaddr *)&serverAddr, sizeof(struct sockaddr)) < 0)
    {
        perror("connect error: ");
        exit(EXIT_FAILURE);
    }

    char buf[1024] = {0};
    if (read(fd, buf, 1024) < 0)
    {
        perror("Read error: ");
        exit(EXIT_FAILURE);
    }

    printf("%s\n", buf);

    close(fd);

    return 0;
}